home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / smail-3.1.28 / util / mkpath.awk < prev    next >
Encoding:
AWK Script  |  1992-07-11  |  6.9 KB  |  364 lines

  1. # @(#)util/mkpath.awk    1.4 7/11/92 11:40:10
  2. #
  3. # mkpath.awk perform awk pre-processing of an input file
  4. #
  5. #    Copyright (C) 1988 Ronald S. Karr and Landon Curt Noll
  6. #    Copyright (C) 1992 Ronald S. Karr
  7. #
  8. # See the file COPYING, distributed with smail, for restriction
  9. # and warranty information.
  10. #
  11. # Some awk's don't process paragraphs in the order that they
  12. # appear in the file, so we are forced to make this program
  13. # non-deterministic.
  14.  
  15. ### initial setup
  16. #
  17. BEGIN {
  18.  
  19. #   clear line counts
  20.     lineno = 0;
  21. #   not in a block initially
  22.     inblock = 0;
  23. #   no errors so far
  24.     errno = 0;
  25. #   line modes
  26.     mode_file = 1;
  27.     mode_cmd = 2;
  28.     mode_lit = 3;
  29.     mode = 0;
  30.  
  31. #   preset shell commands
  32.     gleem = "$GLEEM";
  33. }
  34.  
  35. ### per line debug and processing
  36. #
  37. {
  38.  
  39. #   note another line, echo it if -v
  40.     ++lineno;
  41.     if ( VERBOSE > 0 && NF > 0 ) {
  42.     print PROG": line", lineno":", $0 | "cat 1>&2";
  43.     }
  44. }
  45.  
  46. ### ignore blank lines
  47. #
  48. NF < 1 {
  49.     next;
  50. }
  51.  
  52. ### pre-processing of lines with args
  53. #
  54. NF > 1 {
  55.  
  56. #   find string beyond the first field
  57.     args = substr($0, index($0,$1)+length($1));    # remove field 1
  58.     if ( $2 ~ /^`/ && $NF ~ /`$/ ) {
  59.     mode = mode_cmd;                # cmd output
  60.     # remove backquotes
  61.     args = substr(args, index(args,$2)+1, length(args)-index(args,$2)-1);
  62.     } else {
  63.     if ( $2 ~ /^'/ && $NF ~ /'$/ ) {
  64.         mode = mode_lit;                # text literal
  65.     } else {
  66.         mode = mode_file;                # data file
  67.     }
  68.         args = substr(args, index(args,$2));
  69.     }
  70. }
  71.  
  72. ### map - store map commands into WORK
  73. #
  74. $1 == "map" {
  75.  
  76.     if ( NF <= 1 ) {
  77.     print "echo", PROG, ": line", lineno":", $1, "no args 1>&2";
  78.     errno = 1;
  79.     if (ERR > 0) {
  80.         exit;        # END will be processed
  81.     }
  82.     } else {
  83.     if ( inblock == 0 ) {
  84.         print "(";
  85.         inblock = 1;
  86.     }
  87.     if (mode == mode_cmd) {
  88.         print args, "|", gleem, "-p -c -f -";
  89.     }
  90.     if (mode == mode_lit) {
  91.         print "echo", args "|", gleem, "-p -c -f -";
  92.     }
  93.     if (mode == mode_file) {
  94.         print gleem, "-p -c -d $CWD -f /dev/null", args;
  95.     }
  96.     }
  97.     next;
  98. }
  99.  
  100. ### safemap - store safe map commands into WORK
  101. #
  102. $1 == "safemap" {
  103.  
  104.     if ( NF <= 1 ) {
  105.     print "echo", PROG, ": line", lineno":", $1, "no args 1>&2";
  106.     errno = 11;
  107.     if (ERR > 0) {
  108.         exit;        # END will be processed
  109.     }
  110.     } else {
  111.     if ( inblock == 0 ) {
  112.         print "(";
  113.         inblock = 1;
  114.     }
  115.     if (mode == mode_cmd) {
  116.         print args, "|", gleem, "-s -F -p -c -f -";
  117.     }
  118.     if (mode == mode_lit) {
  119.         print "echo", args "|", gleem, "-s -F -p -c -f -";
  120.     }
  121.     if (mode == mode_file) {
  122.         print gleem, "-s -F -p -c -d $CWD -f /dev/null", args;
  123.     }
  124.     }
  125.     next;
  126. }
  127.  
  128. ### delete - store delete commands into WORK
  129. #
  130. $1 == "delete" {
  131.  
  132.     if ( NF <= 1 ) {
  133.     print "echo", PROG, ": line", lineno":", $1, "no args 1>&2";
  134.     errno = 2;
  135.     if (ERR > 0) {
  136.         exit;        # END will be processed
  137.     }
  138.     } else {
  139.     if ( inblock == 0 ) {
  140.         print "(";
  141.         inblock = 1;
  142.     }
  143.     if (mode == mode_cmd) {
  144.         print args, "|", gleem, "-n delete -f -";
  145.     }
  146.     if (mode == mode_lit) {
  147.         print "echo", args "|", gleem, "-n delete -f -";
  148.     }
  149.     if (mode == mode_file) {
  150.         print gleem, "-p -n delete -d $CWD -f /dev/null", args;
  151.     }
  152.     }
  153.     next;
  154. }
  155.  
  156. ### adjust - store adjust commands into WORK
  157. #
  158. $1 == "adjust" {
  159.  
  160.     if ( NF <= 1 ) {
  161.     print "echo", PROG, ": line", lineno":", $1, "no args 1>&2";
  162.     errno = 3;
  163.     if (ERR > 0) {
  164.         exit;        # END will be processed
  165.     }
  166.     } else {
  167.     if ( inblock == 0 ) {
  168.         print "(";
  169.         inblock = 1;
  170.     }
  171.     if (mode == mode_cmd) {
  172.         print args, "|", gleem, "-n adjust -f -";
  173.     }
  174.     if (mode == mode_lit) {
  175.         print "echo", args "|", gleem, "-n adjust -f -";
  176.     }
  177.     if (mode == mode_file) {
  178.         print gleem, "-p -n adjust -d $CWD -f /dev/null", args;
  179.     }
  180.     }
  181.     next;
  182. }
  183.  
  184. ### dead - store dead commands into WORK
  185. #
  186. $1 == "dead" {
  187.  
  188.     if ( NF <= 1 ) {
  189.     print "echo", PROG, ": line", lineno":", $1, "no args 1>&2";
  190.     errno = 4;
  191.     if (ERR > 0) {
  192.         exit;        # END will be processed
  193.     }
  194.     } else {
  195.     if ( inblock == 0 ) {
  196.         print "(";
  197.         inblock = 1;
  198.     }
  199.     if (mode == mode_cmd) {
  200.         print args, "|", gleem, "-n dead -f -";
  201.     }
  202.     if (mode == mode_lit) {
  203.         print "echo", args "|", gleem, "-n dead -f -";
  204.     }
  205.     if (mode == mode_file) {
  206.         print gleem, "-p -n dead -d $CWD -f /dev/null", args;
  207.     }
  208.     }
  209.     next;
  210. }
  211.  
  212. ### text - store commands to inject
  213. #
  214. $1 == "text" {
  215.  
  216.     if ( NF <= 1 ) {
  217.     print "echo", PROG, ": line", lineno":", $1, "no args 1>&2";
  218.     errno = 5;
  219.     if (ERR > 0) {
  220.         exit;        # END will be processed
  221.     }
  222.     } else {
  223.     if (mode == mode_cmd) {
  224.         print args;
  225.     }
  226.     if (mode == mode_lit) {
  227.         print "echo", args;
  228.     }
  229.     if (mode == mode_file) {
  230.         print "cat /dev/null", args;
  231.     }
  232.     }
  233.     next;
  234. }
  235.  
  236. ### file - store file changing commands into WORK
  237. #
  238. $1 == "file" {
  239.  
  240.     if ( NF > 2 ) {
  241.     print "echo", PROG, ": line", lineno":", $0, "too many args 1>&2";
  242.     errno = 6;
  243.     if (ERR > 0) {
  244.         exit;        # END will be processed
  245.     }
  246.     }
  247.     if ( NF <= 1 ) {
  248.     print "echo", PROG, ": line", lineno":", $1, "no args 1>&2";
  249.     errno = 7;
  250.     if (ERR > 0) {
  251.         exit;        # END will be processed
  252.     }
  253.     }
  254.     if ( NF == 2 ) {
  255.     if ( inblock == 0 ) {
  256.         print "(";
  257.         inblock = 1;
  258.     }
  259.     if ( $2 ~ /^\// ) {
  260.         print "echo \"file {", $2, "}\"";
  261.     } else {
  262.         print "echo \"file { `pwd`/"$2, "}\"";
  263.     }
  264.     }
  265.     next;
  266. }
  267.  
  268. ### cd - store cd directory changing commands into WORK
  269. #
  270. $1 == "cd" {
  271.  
  272.     if ( NF > 2 ) {
  273.     print "echo", PROG, ": line", lineno":", $0, "too many args 1>&2";
  274.     errno = 8;
  275.     if (ERR > 0) {
  276.         exit;        # END will be processed
  277.     }
  278.     }
  279. #   cd by itself refers to the dir from where mkpath was started
  280.     if ( NF == 1 ) {
  281.     print "cd $PWD";
  282.     print "CWD=$PWD";
  283.     }
  284.     if ( NF == 2 ) {
  285. #    cd with a '-' refers to the dir that the input file is in
  286.     if ( $2 == "-" ) {
  287.         print "cd $CD";
  288.         print "CWD=$CD";
  289. #    otherwise cd to the arg
  290.     } else {
  291.         print "cd", $2;
  292.         print "CWD="$2;
  293.     }
  294.     }
  295.     next;
  296. }
  297.  
  298. ### sh - store a SHELL command into a file
  299. #
  300. $1 == "sh" {
  301.  
  302.     if ( NF <= 1 ) {
  303.     print "echo", PROG, ": line", lineno":", $1, "no args 1>&2";
  304.     errno = 9;
  305.     if (ERR > 0) {
  306.         exit;        # END will be processed
  307.     }
  308.     } else {
  309.     print args;
  310.     }
  311.     next;
  312. }
  313.  
  314. ### pathalias - end a block with pathalias(8) plus optional args
  315. #
  316. $1 == "pathalias" {
  317.  
  318.     if ( inblock > 0 ) {
  319.     if ( NF > 1 ) {
  320.         print ") | eval $PATHALIAS", args;
  321.     } else {
  322.         print ") | eval $PATHALIAS";
  323.     }
  324.     }
  325.     inblock = 0;
  326.     next;
  327. }
  328.  
  329. ### pathsort - end a block with pathalias(8) plus post-processing and args
  330. #
  331. $1 == "pathsort" {
  332.  
  333.     if ( inblock > 0 ) {
  334.     if ( NF > 1 ) {
  335.         print ") | eval $PATHSORT", args;
  336.     } else {
  337.         print ") | eval $PATHSORT";
  338.     }
  339.     }
  340.     inblock = 0;
  341.     next;
  342. }
  343.  
  344. ### ERROR - catch unknown lines, known lines resulted in 'next' skipping ERROR
  345. #
  346. {
  347.     errno = 10;
  348.     print PROG": line", lineno": unknown command:", $0 | "cat 1>&2";
  349. #   if the -e flag was igven to mkpath, insert a exit right now
  350.     if (ERR > 0) {
  351.     exit;        # END will be processed
  352.     }
  353. }
  354.  
  355. ### END - the end of a config file has an implied pathsort if it needs one
  356. #
  357. END {
  358.  
  359.     if ( inblock > 0 ) {
  360.     print ") | eval $PATHSORT";
  361.     }
  362.     print "exit", errno;
  363. }
  364.